/**
 * Created by Coing on 2017/2/26.
 */
public class Solution {

    public int strStr(String haystack, String needle) {
        if ( (haystack == null) || (needle == null) ) {
            return -1;
        }
        int haystack_len = haystack.length();
        int needle_len = needle.length();
        if((needle_len == 0) && (needle_len <= haystack_len)){
            return 0;
        }
        if( needle_len > haystack_len){
            return -1;
        }

        for (int i = 0; i < haystack_len - needle_len + 1; i++) {

            for (int j = 0; j < needle_len; j++) {
                if(haystack.charAt(i+j) != needle.charAt(j)){
                    break;
                }
                if(j == (needle_len-1)){
                    return i;
                }
            }
        }
        return -1;
    }
    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.strStr("a", ""));
        System.out.println("**********");
        System.out.println(solution.strStr("", ""));
        System.out.println("**********");
        System.out.println(solution.strStr("abc", "bc"));
        System.out.println("**********");
        System.out.println(solution.strStr("aab", "aa"));
        System.out.println("**********");
        System.out.println(solution.strStr("sdklaff", "ad"));
        System.out.println("**********");
        System.out.println(solution.strStr("mississippi", "issipi"));
        System.out.println("**********");

    }
}
